﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class InvHoldList : BasePage
    {
        protected Button btnDelete;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefurbish;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5052;
        protected GridView gvInvHold;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataInvHoldList(InvHoldQueryEntity invHoldQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(invHoldQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetInvHoldInfoByQueryEntity(invHoldQuery);
            base.BindData2GridView(this.gvInvHold, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> HoldIDs = base.GetSelectedIDsInGridView(this.gvInvHold, "INV_HOLD_ID");
                List<string> IsHold = base.GetSelectedIDsInGridView(this.gvInvHold, "IS_HOLD");
                if ((HoldIDs == null) || (HoldIDs.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    for (int i = 0; i < IsHold.Count; i++)
                    {
                        if (IsHold[i] == "Y")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5250_007");
                            return;
                        }
                    }
                    this.lblMsg.Text = InventoryMgtServiceClientProxy.GetInstance().DeleteInvHold(HoldIDs) + base.GetGlobalMsgResource("C006");
                    InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
                    this.BindDataInvHoldList(invHoldQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
            invHoldQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = invHoldQuery.CurrentPage;
            invHoldQuery.CurrentPage = "0";
            this.gvInvHold.Columns[0].Visible = false;
            this.gvInvHold.AllowSorting = false;
            this.BindDataInvHoldList(invHoldQuery);
            this.hdnActionID.Value = "";
            this.gvInvHold.ShowFooter = false;
            invHoldQuery.IsGetAll = false;
            invHoldQuery.CurrentPage = currentPageIndex;
            this.SetInvHoldQueryEntity(invHoldQuery);
            base.Export2Excel("InvHold.xls", this.gvInvHold);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
            invHoldQuery.CurrentPage = "0";
            this.BindDataInvHoldList(invHoldQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
            invHoldQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataInvHoldList(invHoldQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
            invHoldQuery.InvHoldID = "";
            invHoldQuery.Lot = "";
            invHoldQuery.LpnID = "";
            invHoldQuery.Loc = "";
            invHoldQuery.OwnerID = "";
            invHoldQuery.SkuID = "";
            invHoldQuery.HoldReasonCode = "";
            invHoldQuery.HoldBy = "";
            invHoldQuery.HoldDate = "";
            invHoldQuery.ReleasedBy = "";
            invHoldQuery.IsHold = "";
            this.BindDataInvHoldList(invHoldQuery);
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("InvHold.aspx", false);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
            if (int.Parse(invHoldQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invHoldQuery.CurrentPage = (int.Parse(invHoldQuery.CurrentPage) + 1).ToString();
                this.BindDataInvHoldList(invHoldQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
            if (int.Parse(invHoldQuery.CurrentPage) > 0)
            {
                invHoldQuery.CurrentPage = (int.Parse(invHoldQuery.CurrentPage) - 1).ToString();
                this.BindDataInvHoldList(invHoldQuery);
            }
        }

        private InvHoldQueryEntity GetInvHoldQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as InvHoldQueryEntity);
            }
            return (base.PageQueryEntitySession as InvHoldQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvHoldQueryEntity invHoldQuery = this.GetInvHoldQueryEntity();
                invHoldQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataInvHoldList(invHoldQuery);
            }
        }

        protected void gvInvHold_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5052", this.gvInvHold);
        }

        protected void gvInvHold_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvInvHold_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvHoldQueryEntity InvHoldQuery = this.GetInvHoldQueryEntity();
                InvHoldQuery.SortField = e.SortExpression;
                if (InvHoldQuery.SortDirection == "ASC")
                {
                    InvHoldQuery.SortDirection = "DESC";
                }
                else if (InvHoldQuery.SortDirection == "DESC")
                {
                    InvHoldQuery.SortDirection = "ASC";
                }
                this.BindDataInvHoldList(InvHoldQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InvHoldQueryEntity invHoldQuery = new InvHoldQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invHoldQuery.InvHoldID = this.PreviousPage.InvHoldID;
                    invHoldQuery.Lot = this.PreviousPage.Lot;
                    invHoldQuery.LpnID = this.PreviousPage.LpnID;
                    invHoldQuery.Loc = this.PreviousPage.Loc;
                    invHoldQuery.OwnerID = this.PreviousPage.OwnerID;
                    invHoldQuery.SkuID = this.PreviousPage.SkuID;
                    invHoldQuery.HoldReasonCode = this.PreviousPage.HoldReasonCode;
                    invHoldQuery.HoldBy = this.PreviousPage.HoldBy;
                    invHoldQuery.HoldDate = this.PreviousPage.HoldDate;
                    invHoldQuery.ReleasedBy = this.PreviousPage.ReleasedBy;
                    invHoldQuery.IsHold = this.PreviousPage.IsHold;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            invHoldQuery.IsGetAll = false;
            invHoldQuery.CurrentPage = "0";
            invHoldQuery.PageSize = this.gvInvHold.PageSize.ToString();
            invHoldQuery.SortField = "INV_HOLD_ID";
            invHoldQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvInvHold.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetInvHoldQueryEntity(invHoldQuery);
            this.BindDataInvHoldList(invHoldQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetInvHoldQueryEntity(InvHoldQueryEntity invHoldQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = invHoldQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = invHoldQueryEntity;
            }
        }

        public InvHoldQuery PreviousPage
        {
            get
            {
                return (InvHoldQuery) base.PreviousPage;
            }
        }
    }
}

